﻿create procedure dbo.sp_Proveedor_Search
@Prefix varchar(15) = null,
@Count int = 0
as
begin
set nocount on

declare @proveedores table(Rut varchar(15), Nombre varchar(50))
insert into @proveedores select Rut, Nombre from TB_Proveedor
insert into @proveedores select RutProveedor, NombreProveedor from Tb_Gastos

;with proveedores as (select row_number() over(order by Rut asc) as row
								,Rut
								,Nombre
						from	@proveedores
						where		((@Prefix is null or @Prefix = '') 
										or (Rut like '%' + @Prefix + '%'))
						group by Rut, Nombre)
select	 Rut
		,Nombre
from	proveedores						
where	((@count is null or @count = 0) or (row  <= @count))

end
